<?php

namespace App\Http\Models\Report;

use App\Http\Models\Business\InvoiceTargetContractYear;
use App\Http\Models\Report\InvoiceRegionContract;
use App\Http\Models\Business\CacheInvoiceRegionContract;

class ReportInvoiceRegionContract
{
    public $regions = [];
    public $year = 0;
    public $month = 0;
    public $cacheInvoiceRegionContracts = null;
    public $invoiceTargetContractYears = null;

    //合计过的分合同大类的年度指标
    private $totalInvoiceTargetYearByContract = [];
    //合计过的分合同大类的年度实际开票
    private $totalInvoiceRealYearByContract = [];

    public function __construct(int $year, int $month)
    {
        $this->year = $year;
        $this->month = $month;
        $invoice_sales_regions = InvoiceRegionContract::all();
        $cacheInvoiceRegionContracts = CacheInvoiceRegionContract::where(['year' => $this->year])->get();
        $invoiceTargetContractYears = InvoiceTargetContractYear::where(['year' => $this->year])->get();
        $this->regions = $invoice_sales_regions;
        $this->cacheInvoiceRegionContracts = $cacheInvoiceRegionContracts;
        $this->invoiceTargetContractYears = $invoiceTargetContractYears;
        $this->assign_data();
    }


    // public function addRegion(InvoiceRegionContract $region)
    // {
    //     if (!array_key_exists($region->code, $this->regions)) {
    //         $this->regions[$region->code] = $region;
    //     }
    // }

    //给各个大区分配数据
    private function assign_data()
    {
        foreach ($this->regions as $region) {
            //分配缓存过的开票数据、分合同大类、分月份
            foreach ($this->cacheInvoiceRegionContracts as $value) {
                if ($region->code == $value->sales_region_code) {
                    $region->cacheInvoiceRegionContracts[] = $value;
                }
            }

            //分配指标数据，分合同大类
            foreach ($this->invoiceTargetContractYears as $value) {
                if ($region->code == $value->sales_region_code  && !array_key_exists($value->category_contract_id, $region->target_invoice)) {
                    $region->target_invoice[$value->category_contract_id] = $value;
                }
            }

            $region->set_real_invoice_total_contract_year();
        }

        $this->setTotalInvoiceTargetYearByContract();
        $this->setTotalInvoiceRealYearByContract();
    }


    //获取当月各大区开票分类合计
    public function getTotalInvoiceMonthContractById(int $category_contract_id)
    {
        $result = 0;
        foreach ($this->cacheInvoiceRegionContracts as $value) {
            if ($value->month == $this->month && $value->category_contract_id == $category_contract_id) {
                $result += $value->amount;
            }
        }
        return $result;
    }


    //获取年度汇总指标，分合同大类
    public function getTotalInvoiceTargetYearByContract(int $category_contract_id)
    {
        $result = 0;
        if (array_key_exists($category_contract_id, $this->totalInvoiceTargetYearByContract)) {
            $result = $this->totalInvoiceTargetYearByContract[$category_contract_id];
        }
        return $result;
    }

    //获取年度汇总实际开票数量，分合同大类
    public function getTotalInvoiceRealYearByContract(int $category_contract_id)
    {
        $result = 0;
        if (array_key_exists($category_contract_id, $this->totalInvoiceRealYearByContract)) {
            $result = $this->totalInvoiceRealYearByContract[$category_contract_id];
        }
        return $result;
    }


    //根据合同大类获取总的全年开票进度
    public function totalInvoiceProgressByContract(int $category_contract_id)
    {
        $invoice_real = 0;
        if (array_key_exists($category_contract_id, $this->totalInvoiceRealYearByContract)) {
            $invoice_real = $this->totalInvoiceRealYearByContract[$category_contract_id];
        }

        $invoice_target = 0;
        if (array_key_exists($category_contract_id, $this->totalInvoiceTargetYearByContract)) {
            $invoice_target = $this->totalInvoiceTargetYearByContract[$category_contract_id];
        }

        $progress = 0;
        if ($invoice_target != 0) {
            $progress = $invoice_real / $invoice_target;
        }
        return $progress;
    }

    //根据合同大类数组获取总的全年开票进度
    public function totalInvoiceProgressByContracts(array $category_contract_ids)
    {
        $invoice_real = 0;
        foreach ($this->totalInvoiceRealYearByContract as $category_contract_id => $value) {
            if (in_array($category_contract_id, $category_contract_ids)) {
                $invoice_real += $value;
            }
        }

        $invoice_target = 0;
        foreach ($this->totalInvoiceTargetYearByContract as $category_contract_id => $value) {
            if (in_array($category_contract_id, $category_contract_ids)) {
                $invoice_target += $value;
            }
        }

        $progress = 0;
        if ($invoice_target != 0) {
            $progress = $invoice_real / $invoice_target;
        }
        return $progress;
    }

    //设置年度指标汇总，分合同大类
    private function setTotalInvoiceTargetYearByContract()
    {
        foreach ($this->invoiceTargetContractYears as $value) {
            if (array_key_exists($value->category_contract_id, $this->totalInvoiceTargetYearByContract)) {
                $this->totalInvoiceTargetYearByContract[$value->category_contract_id] += $value->amount;
            } else {
                $this->totalInvoiceTargetYearByContract[$value->category_contract_id] = $value->amount;
            }
        }
    }


    //设置年度实际开票汇总，分合同大类
    private function setTotalInvoiceRealYearByContract()
    {
        foreach ($this->cacheInvoiceRegionContracts as $value) {
            if (array_key_exists($value->category_contract_id, $this->totalInvoiceRealYearByContract)) {
                $this->totalInvoiceRealYearByContract[$value->category_contract_id] += $value->amount;
            } else {
                $this->totalInvoiceRealYearByContract[$value->category_contract_id] = $value->amount;
            }
        }
    }
}
